package com.sbck.cs3.MyClass;

import com.google.gson.Gson;
import com.sbck.cs3.LoginUser;
import com.sbck.cs3.MyGlobal;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.util.Objects;

public class FileUtils {
    public static void SaveLoginInfo()
    {
        Gson gson = new Gson();
        String str = gson.toJson(MyGlobal.loginUser);
        File file = new File("data/data/com.sbck.cs3/login.txt");
        try {
            FileOutputStream fileOutputStream = new FileOutputStream(file);
            fileOutputStream.write(str.getBytes());
            fileOutputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void ReadLoginInfo()
    {
        Gson gson = new Gson();
        File file = new File("data/data/com.sbck.cs3/login.txt");
        try {
            FileInputStream fileInputStream = new FileInputStream(file);
            BufferedReader reader = new BufferedReader(new InputStreamReader(fileInputStream));
            String str = reader.readLine();
            if(Objects.isNull(str))
                return;
            fileInputStream.close();
            LoginUser loginUser = gson.fromJson(str,LoginUser.class);
            if(loginUser.IsChecked.get())
                MyGlobal.loginUser = loginUser;
            else
                MyGlobal.loginUser = new LoginUser();
        } catch (Exception e) {
            e.printStackTrace();
            MyGlobal.loginUser = new LoginUser();
        }
    }
}
